//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "ShoutCondition.h"
using namespace Cephei::QL::Methods::Finitedifferences;
#include <gen/QL/Math/Array.h>
using namespace Cephei::QL::Math;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Methods::Finitedifferences::CShoutCondition::CShoutCondition (Cephei::QL::Math::IArray^ intrinsicValues, Double resTime, Double rate) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CArray^ _CintrinsicValues;
    try
    {
#ifdef HANDLE
        _phShoutCondition = NULL;
#endif
        _CintrinsicValues = safe_cast<CArray^> (intrinsicValues);
        _CintrinsicValues->Lock();
        QuantLib::Array& _intrinsicValues = static_cast<QuantLib::Array&> (_CintrinsicValues->GetReference ()); 
        QuantLib::Time _resTime = (QuantLib::Time)ValueHelper::Convert (resTime);
        QuantLib::Rate _rate = (QuantLib::Rate)ValueHelper::Convert (rate);
        _ppShoutCondition = new boost::shared_ptr<QuantLib::ShoutCondition> (new QuantLib::ShoutCondition ( _intrinsicValues,  _resTime,  _rate ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_CintrinsicValues != nullptr) _CintrinsicValues->Unlock();
    }
}
Cephei::QL::Methods::Finitedifferences::CShoutCondition::CShoutCondition (QL::Option::TypeEnum type, Double strike, Double resTime, Double rate) 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phShoutCondition = NULL;
#endif
        QuantLib::Option::Type _type = (QuantLib::Option::Type)type ;
        QuantLib::Real _strike = (QuantLib::Real)ValueHelper::Convert (strike);
        QuantLib::Time _resTime = (QuantLib::Time)ValueHelper::Convert (resTime);
        QuantLib::Rate _rate = (QuantLib::Rate)ValueHelper::Convert (rate);
        _ppShoutCondition = new boost::shared_ptr<QuantLib::ShoutCondition> (new QuantLib::ShoutCondition ( _type,  _strike,  _resTime,  _rate ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Methods::Finitedifferences::CShoutCondition::CShoutCondition (boost::shared_ptr<QuantLib::ShoutCondition>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phShoutCondition = NULL;
#endif
	_ppShoutCondition = &childNative;
    
}
Cephei::QL::Methods::Finitedifferences::CShoutCondition::CShoutCondition (QuantLib::ShoutCondition& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phShoutCondition = NULL;
#endif
	_ppShoutCondition = new boost::shared_ptr<QuantLib::ShoutCondition> (&childNative);
    
    _ShoutConditionOwner = owner;
    
}

Cephei::QL::Methods::Finitedifferences::CShoutCondition::CShoutCondition (CShoutCondition^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phShoutCondition = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppShoutCondition = new boost::shared_ptr<QuantLib::ShoutCondition> (copy->GetShared());
        
    }
}
Cephei::QL::Methods::Finitedifferences::CShoutCondition::CShoutCondition (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phShoutCondition = NULL;
#endif
	if (!t->IsSubclassOf(CShoutCondition::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Methods::Finitedifferences::CShoutCondition::CShoutCondition (QuantLib::Handle<QuantLib::ShoutCondition>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phShoutCondition = &childNative;
	_ppShoutCondition = &static_cast<boost::shared_ptr<QuantLib::ShoutCondition>>(childNative.currentLink());
    
    _ShoutConditionOwner = owner;
}
Cephei::QL::Methods::Finitedifferences::CShoutCondition::CShoutCondition (QuantLib::Handle<QuantLib::ShoutCondition> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phShoutCondition = &childNative;
	_ppShoutCondition = &static_cast<boost::shared_ptr<QuantLib::ShoutCondition>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Methods::Finitedifferences::CShoutCondition::CShoutCondition (QuantLib::ShoutCondition childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phShoutCondition = NULL;
#endif
	_ppShoutCondition = new boost::shared_ptr<QuantLib::ShoutCondition> (new QuantLib::ShoutCondition (childNative));
    
}
#endif

Cephei::QL::Methods::Finitedifferences::CShoutCondition::~CShoutCondition ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppShoutCondition != NULL)
    {
	    delete _ppShoutCondition;
        _ppShoutCondition = NULL;
    }
}
Cephei::QL::Methods::Finitedifferences::CShoutCondition::!CShoutCondition ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppShoutCondition != NULL)
    {
	    delete _ppShoutCondition;
    }
}
QuantLib::ShoutCondition& Cephei::QL::Methods::Finitedifferences::CShoutCondition::GetReference ()
{
    if (_ppShoutCondition == NULL) throw gcnew NativeNullException ();
	return **_ppShoutCondition;
}
boost::shared_ptr<QuantLib::ShoutCondition>& Cephei::QL::Methods::Finitedifferences::CShoutCondition::GetShared ()
{
    if (_ppShoutCondition == NULL) throw gcnew NativeNullException ();
	return *_ppShoutCondition;
}
QuantLib::ShoutCondition* Cephei::QL::Methods::Finitedifferences::CShoutCondition::GetPointer ()
{
    if (_ppShoutCondition == NULL) throw gcnew NativeNullException ();
	return &**_ppShoutCondition;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::ShoutCondition>& Cephei::QL::Methods::Finitedifferences::CShoutCondition::GetHandle ()
{
	if (_phShoutCondition == NULL)
	{
		_phShoutCondition = new Handle<QuantLib::ShoutCondition> (*_ppShoutCondition);
	}
	return *_phShoutCondition;
}
#endif
bool Cephei::QL::Methods::Finitedifferences::CShoutCondition::HasNative () 
{
	return (_ppShoutCondition != NULL);
}

Cephei::QL::Methods::Finitedifferences::IShoutCondition^ Cephei::QL::Methods::Finitedifferences::CShoutCondition::ApplyTo (Cephei::QL::Math::IArray^ a, Double t)
{
    CArray^ _Ca;
    try
    {
        _Ca = safe_cast<CArray^> (a);
        _Ca->Lock();
        QuantLib::Array& _a = static_cast<QuantLib::Array&> (_Ca->GetReference ()); 
        QuantLib::Time _t = (QuantLib::Time)ValueHelper::Convert (t);
    	(*_ppShoutCondition)->applyTo ( _a,  _t );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ca != nullptr) _Ca->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Methods::Finitedifferences::IShoutCondition^ Cephei::QL::Methods::Finitedifferences::CShoutCondition_Factory::Create (Cephei::QL::Math::IArray^ intrinsicValues, Double resTime, Double rate)
{
    return gcnew CShoutCondition ( intrinsicValues,  resTime,  rate);
}
Cephei::QL::Methods::Finitedifferences::IShoutCondition^ Cephei::QL::Methods::Finitedifferences::CShoutCondition_Factory::Create (QL::Option::TypeEnum type, Double strike, Double resTime, Double rate)
{
    return gcnew CShoutCondition ( type,  strike,  resTime,  rate);
}
